package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.service.WorkFlowProductService;
import com.qidianit.mes.pro.vo.WorkflowProductCategory;
import com.qidianit.mes.pro.vo.WorkflowProductRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/19 - 16:34
 */
@RestController
@RequestMapping("/workflowProduct")
public class WorkFlowProductController {

    @Autowired
    private WorkFlowProductService workFlowProductService;

    @GetMapping("/page")
    public Result<PageInfo<WorkflowProductRelation>> queryProductsByProcessCodeAndPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                                                       @RequestParam(value = "size",defaultValue = "10")Integer size,
                                                                                       @RequestParam("workflowId") Integer workflowId)
    {
        return Result.success(workFlowProductService.queryProductsByProcessCodeAndPageInteger(page,size,workflowId));
    }

    @PostMapping("/add")
    public Result addWorkFlowProductRelation(@RequestBody WorkflowProductRelation workflowProductRelation) {
        workflowProductRelation.setCreateTime(LocalDateTime.now());
        workflowProductRelation.setCreateBy("1001");
        int result = workFlowProductService.addWorkFlowProductRelation(workflowProductRelation);
        return result == 1?Result.success():result == -1?Result.error("该产品/物料已经添加"):Result.error("操作失败");
    }

    @GetMapping("/{id}")
    public Result<WorkflowProductCategory> getWorkFlowProductRelation(@PathVariable("id") Integer id) {
        return Result.success(workFlowProductService.queryWorkflowAndProductsAndCategoryById(id));
    }

    @PutMapping("/update")
    public Result updateWorkFlowProductRelation(@RequestBody WorkflowProductRelation workflowProductRelation) {
        int result = workFlowProductService.updateWorkFlowProductRelation(workflowProductRelation);
        return result == 1?Result.success():result == -1?Result.error("该产品/物料已经添加过"):Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteWorkFlowProductRelation(@PathVariable("id") Integer id) {

        return workFlowProductService.deleteWorkFlowProductRelationById(id) == 1? Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/batchDelete")
    public Result batchDeleteWorkFlowProductRelation(@RequestParam("id") List<Integer> ids) {
        int result = workFlowProductService.deleteWorkFlowProductRelationByIds(ids);
        return result == 1?Result.success():result == -1?Result.error("删除了部分"):Result.error("操作失败");
    }

}
